package mall.controller.mall;

import com.google.gson.Gson;
import mall.model.Result;
import mall.model.User;
import mall.model.bo.AdminLoginBO;
import mall.model.bo.MallUpdatePwdBO;
import mall.model.bo.MallUpdateUserDataBO;
import mall.model.bo.MallUserSignupBO;
import mall.model.vo.MallShowDataVO;
import mall.model.vo.MallUserSignupVO;
import mall.model.vo.MallUserloginVO;
import mall.service.MallUserService;
import mall.service.MallUserServiceImpl;
import mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 11:27
 *
 * */
@WebServlet("/api/mall/user/*")
public class UserServlet extends HttpServlet {
    private Gson gson = new Gson();
    private MallUserService mallUserService = new MallUserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if ("signup".equals(action)) {
            signup(request, response);
        } else if ("login".equals(action)) {
            login(request, response);
        } else if ("updatePwd".equals(action)) {
            updatePwd(request, response);
        } else if ("updateUserData".equals(action)) {
            updateUserData(request, response);
        }

    }

    /**
     * 修改用户信息
     * 1. 获取请求参数{id: "2", nickname: "yonni", recipient: "国勇", address: "四川省泸州市古蔺县", phone: "18342205165"}
     * 2. 处理业务逻辑
     * 3. 作出响应{"code":0}
     * */
    private void updateUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MallUpdateUserDataBO userDataBO = gson.fromJson(requestBody, MallUpdateUserDataBO.class);
        mallUserService.updateUserData(userDataBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 更新密码
     * 1. 获取请求参数{id: "530", oldPwd: "Aa&12345", newPwd: "Aa%12345", confirmPwd: "Aa%12345"}
     * 2. 处理业务逻辑，更新数据库表
     * 3. 作出响应
     *          旧密码不正确：{"code":10000,"message":"旧密码不正确!"}
     *          成功：{"code":0}
     *          其余情况前端已处理，直接返回即可(如两次输入密码不一致)
     * */
    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MallUpdatePwdBO mallUpdatePwdBO = gson.fromJson(requestBody, MallUpdatePwdBO.class);
        Map<String, Object> result = mallUserService.updatePwd(mallUpdatePwdBO);
        //旧密码错误
        if (!(boolean)result.get("flag")) {
            response.getWriter().println(gson.toJson(Result.error((String) result.get("message"))));
            return;
        }
        //密码修改成功
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /*
    * 用户登录
    * 1. 获取请求参数{email: "yonni@qq.com", pwd: "0527"}
    * 2. 处理业务逻辑
    * 3. 作出响应
    *      成功：     {"code":10000,"message":"账号或密码不正确!"}
    *      失败：     {"code":0,"data":{"code":0,"name":"adminn","token":"adminn"}}
    * */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminLoginBO loginBO = gson.fromJson(requestBody, AdminLoginBO.class);
        User user = mallUserService.login(loginBO);
        if (user != null) {
            MallUserloginVO loginVO = new MallUserloginVO();
            loginVO.setName(user.getNickname());
            loginVO.setToken(user.getNickname());
            response.getWriter().println(gson.toJson(Result.ok(loginVO)));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("账号与密码不匹配")));
    }

    /**
     * 用户注册
     * 1. 获取请求参数
     * 2. 处理业务逻辑
     * 3. 作出响应 {"code":0,"data":{"code":0,"name":"ahfoah","token":"ahfoah"}}
     * */
    private void signup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MallUserSignupBO signupBO = gson.fromJson(requestBody, MallUserSignupBO.class);
        MallUserSignupVO signupVO = mallUserService.signup(signupBO);
        response.getWriter().println(gson.toJson(Result.ok(signupVO)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if ("data".equals(action)) {
            showData(request, response);
        }
    }

    /**
     * 个人中心修改资料(显示用户信息)
     * 1. 获取请求数据token: Yonni
     * 2. 处理业务逻辑：数据库表获取数据
     * 3. 作出响应
     * */
    private void showData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        MallShowDataVO dataVO = mallUserService.showData(token);
        response.getWriter().println(gson.toJson(Result.ok(dataVO)));
    }
}
